<?php

class SgatheringController extends AdminCController
{
	public $defaultAction = 'list';
	
	public function actionCreate()
	{
		$url = $this -> _getUrl();
		$source_from = $this -> _getSource();
		
		$news_stuct =$this -> _getStruct($source_from, $url);
		
		$post = new Post();
		$post -> title = iconv('gbk', 'utf-8', $news_stuct['title']) ;
		$post -> content = iconv('gbk', 'utf-8', $news_stuct['content']) ;
		$post -> author_id = 1;
		$post -> tags = $source_from;
		$post -> create_time = time();
		$post -> status = 2;
		
		if($post -> validate()){
			echo $post -> save();
		}else{
			var_dump($post -> errors);
			die;
		}
		$this -> redirect($this -> createUrl('/sadmin/sgathering/list'));
		$this->render('create');
	}
	
	
	protected function _getTitltExp(){
		
	}
	
	protected  function _getStruct($key,$url){
		
		$file_content = file_get_contents($url);
		$title = '';
		switch($key){
			case "1":
				
				$preg_string = 'bossZone\=\"content\"\>(.*?)\<span\sstyle\=\"width\:0\;height\:0\;';
				$preg_partten = '/'.$preg_string.'/s';
				preg_match_all($preg_partten, $file_content, $all);
				$content = $all[1][0];
				
				$title ='/\"hd\"\>
\<h1\>(.*?)\<\/h1\>/s';
				preg_match_all($title, $file_content, $all);
				$title = $all[1][0];
				
				break;
				
			case 2:
				$preg_string = 'BEGIN\-\-\>(.*?)\<\!\-\-';
				$preg_partten = '/'.$preg_string.'/s';
				preg_match_all($preg_partten, $file_content, $all);
				$content = $all[1][0];
				
				$title ='/artibodyTitle\".*?>(.*?)\</s';
				preg_match_all($title, $file_content, $all);
				
				$title = $all[1][0];
				break;
			case 3:
				
				break;
			default :
				return false;
		}
		
		return array('title'=>$title,'content'=>$content);
	}
	
	
	protected function _getUrl(){
		$req = Yii::app() -> request ;
		return $req -> getParam('url');
	}
	
	protected function _getSource(){
		$req = Yii::app() -> request ;
		return $req -> getParam('source_from');
	}

	public function actionDelete()
	{
		$this->render('delete');
	}

	public function actionIndex()
	{
		$this->render('index');
	}

	public function actionList()
	{
		$this->render('list');
	}

	public function actionUpdate()
	{
		$this->render('update');
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}